package com.vrv.netmgr.entry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.vrv.common.adapter.VRVExpandableListAdapter;
import com.vrv.common.util.DataSetUtil;
import com.vrv.common.util.StringUtils;
import com.vrv.netmgr.R;
import com.vrv.netmgr.persistent.LoginSharedPreferencesHelper;
import com.vrv.netmgr.vo.LoginInfo;

/**
 * @Description 系统配置导航主界面
 *
 * @author gaopei
 * @date
 */
public class ConfigActivity extends Activity {
	private boolean isBack;

	private Boolean isNavgate = Boolean.TRUE;

	private Boolean isTop = true;

	private LinearLayout leverTwoLayout;

	private ExpandableListAdapter mAdapter;

	private ExpandableListView expandListView;

	private List<Map<String, String>> groups = new ArrayList<Map<String, String>>();

	private List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initViewSetting();
	}
	/**
	 * 描述：接受广播，关闭页面
	 */
	protected BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			finish();
		}
	};
	/**
	 * 描述：在当前页面注册广播
	 */
	public void onResume(){
		super.onResume();
		if (isNavgate) {
			initViewSetting();
		} else {
			isNavgate = Boolean.TRUE;
		}
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction("exitActivity");
		this.registerReceiver(this.broadcastReceiver, intentFilter);
	}
	/**
	 * 描述：页面销毁时注销广播
	 */
	protected void onDestroy(){
		super.onDestroy();
		this.unregisterReceiver(broadcastReceiver);
	}
	/**
	 * 加载初始化布局，初始化组件
	 */
	public void initViewSetting() {

		isTop = Boolean.TRUE;

		setContentView(R.layout.config_nine_cell);
		leverTwoLayout = (LinearLayout) this.findViewById(R.id.lever_2_layout);
		leverTwoLayout.setVisibility(View.VISIBLE);

		MainTab mainTab = (MainTab) getParent();
		mainTab.setTitle(getResources().getString(R.string.configuration), false);

		RelativeLayout nav1Layout = (RelativeLayout) this.findViewById(R.id.navCfgMgr);
		nav1Layout.setTag(Boolean.FALSE);
		nav1Layout.setBackgroundColor(getResources().getColor(R.color.gray1));
		TextView bubble0 = (TextView) findViewById(R.id.commentWidget_0);
		bubble0.setVisibility(View.VISIBLE);

		RelativeLayout nav2Layout = (RelativeLayout) this.findViewById(R.id.navSysMgr);
		nav2Layout.setTag(Boolean.TRUE);
		TextView bubble1 = (TextView) findViewById(R.id.commentWidget_1);
		bubble1.setVisibility(View.GONE);

//		RelativeLayout nav3Layout = (RelativeLayout) this.findViewById(R.id.navSysMaintenance);
//		nav3Layout.setTag(Boolean.FALSE);
//		nav3Layout.setBackgroundColor(getResources().getColor(R.color.gray1));
//		TextView bubble2 = (TextView) findViewById(R.id.commentWidget_2);
//		bubble2.setVisibility(View.VISIBLE);

		//系统设置
		RelativeLayout sysSetLayout = (RelativeLayout) this.findViewById(R.id.sysSet);
		nav2Layout.setTag(Boolean.TRUE);
		bubble1.setVisibility(View.GONE);

		nav1Layout.setOnClickListener(new ClickLLListener());
		nav2Layout.setOnClickListener(new ClickLLListener());
//		nav3Layout.setOnClickListener(new ClickLLListener());
		sysSetLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(ConfigActivity.this, SystemSetting.class));
			}
		});

	}

	/**
	 * 切换到第三级布局
	 */
	public void changeToLever3ViewSetting() {
		isTop = Boolean.FALSE;

		setContentView(R.layout.lever_config_nine_cell);// 切换布局
		expandListView = (ExpandableListView) findViewById(R.id.expand_list_view);
		mAdapter = new VRVExpandableListAdapter(ConfigActivity.this, groups, R.layout.groups, childs, R.layout.childs);
		expandListView.setAdapter(mAdapter);
		expandListView.setOnGroupClickListener(new OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

				Map<String, String> groupItem = groups.get(groupPosition);
				String activityName = groupItem.get("activityName");

				if (StringUtils.isBlank(activityName))
					return true;

				ComponentName componentName = new ComponentName(ConfigActivity.this, activityName);
				Intent intent = new Intent();
				intent.setComponent(componentName);
				isNavgate = Boolean.FALSE;
				overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
				startActivity(intent);

				return false;
			}
		});
	}

	private class ClickLLListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// 1.初始化数据
			switch (v.getId()) {
			case R.id.navCfgMgr:

				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;

				fillLever3DataSet(0, getResources().getString(R.string.strCfgMgr));
				break;
			case R.id.navSysMgr:

				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;

				fillLever3DataSet(1, getResources().getString(R.string.strSysMgr));
				break;
//			case R.id.navSysMaintenance:
//
//				if (!Boolean.valueOf(String.valueOf(v.getTag())))
//					return;
//
//				fillLever3DataSet(2, getResources().getString(R.string.strSysMaintenance));
//				break;

			default:
				break;
			}

			// 2.切换布局填充adapter
			changeToLever3ViewSetting();
		}
	}

	/**
	 * 初始化静态数据
	 *
	 * @param string
	 *
	 * @param i
	 */
	private void fillLever3DataSet(int index, String title) {

		MainTab mainTab = (MainTab) getParent();
		mainTab.setTitle(title, true);
		mainTab.getBackBtn().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		groups.clear();
		if (index == 0) {
			// [start] 配置管理
			Map<String, String> groupMap0 = new HashMap<String, String>();
			groupMap0.put("key", "0");
			groupMap0.put("group", "快速配置");
			groupMap0.put("activityName", "com.vrv.netmgr.assets.AssetsQryActivity");
			groups.add(groupMap0);

			Map<String, String> groupMap1 = new HashMap<String, String>();
			groupMap1.put("key", "0");
			groupMap1.put("group", "系统配置");
			groupMap1.put("activityName", "");
			groups.add(groupMap1);

			Map<String, String> groupMap2 = new HashMap<String, String>();
			groupMap2.put("key", "0");
			groupMap2.put("group", "注册程序配置");
			groupMap2.put("activityName", "");
			groups.add(groupMap2);
			// [end] 配置管理
		} else if (index == 1) {
			// [start] 配置管理
			Map<String, String> groupMap3 = new HashMap<String, String>();
			groupMap3.put("key", "1");
			groupMap3.put("group", "用户管理");
			groupMap3.put("activityName", "");
			groups.add(groupMap3);

			Map<String, String> groupMap4 = new HashMap<String, String>();
			groupMap4.put("key", "1");
			groupMap4.put("group", "权限角色");
			groupMap4.put("activityName", "");
			groups.add(groupMap4);

			Map<String, String> groupMap5 = new HashMap<String, String>();
			groupMap5.put("key", "1");
			groupMap5.put("group", "设备自定义组");
			groupMap5.put("activityName", "");
			groups.add(groupMap5);

			Map<String, String> groupMap6 = new HashMap<String, String>();
			groupMap6.put("key", "1");
			groupMap6.put("group", "用户设置");
			groupMap6.put("activityName", "com.vrv.netmgr.config.UserSettingsActivity");
			groups.add(groupMap6);

			Map<String, String> groupMap7 = new HashMap<String, String>();
			groupMap7.put("key", "1");
			groupMap7.put("group", "业务导航");
			groupMap7.put("activityName", "");
			groups.add(groupMap7);
			// [end] 配置管理
		} else if (index == 2) {
			// [start] 系统维护
			Map<String, String> groupMap8 = new HashMap<String, String>();
			groupMap8.put("key", "2");
			groupMap8.put("group", "系统运维监视");
			groupMap8.put("activityName", "");
			groups.add(groupMap8);

			Map<String, String> groupMap9 = new HashMap<String, String>();
			groupMap9.put("key", "2");
			groupMap9.put("group", "系统推送设置");
			groupMap9.put("activityName", "");
			groups.add(groupMap9);

			Map<String, String> groupMap10 = new HashMap<String, String>();
			groupMap10.put("key", "2");
			groupMap10.put("group", "软件授权许可证设置");
			groupMap10.put("activityName", "");
			groups.add(groupMap10);

			Map<String, String> groupMap11 = new HashMap<String, String>();
			groupMap11.put("key", "2");
			groupMap11.put("group", "操作系统序列号管理");
			groupMap11.put("activityName", "");
			groups.add(groupMap11);

			Map<String, String> groupMap12 = new HashMap<String, String>();
			groupMap12.put("key", "2");
			groupMap12.put("group", "数据库配置");
			groupMap12.put("activityName", "");
			groups.add(groupMap12);

			Map<String, String> groupMap13 = new HashMap<String, String>();
			groupMap13.put("key", "2");
			groupMap13.put("group", "数据重整");
			groupMap13.put("activityName", "");
			groups.add(groupMap13);

			Map<String, String> groupMap14 = new HashMap<String, String>();
			groupMap14.put("key", "2");
			groupMap14.put("group", "数字证书映射情况");
			groupMap14.put("activityName", "");
			groups.add(groupMap14);

			Map<String, String> groupMap15 = new HashMap<String, String>();
			groupMap15.put("key", "2");
			groupMap15.put("group", "工具上传管理");
			groupMap15.put("activityName", "");
			groups.add(groupMap15);

			Map<String, String> groupMap16 = new HashMap<String, String>();
			groupMap16.put("key", "2");
			groupMap16.put("group", "普通工具下载");
			groupMap16.put("activityName", "");
			groups.add(groupMap16);

			Map<String, String> groupMap17 = new HashMap<String, String>();
			groupMap17.put("key", "2");
			groupMap17.put("group", "数字证书审计上报");
			groupMap17.put("activityName", "");
			groups.add(groupMap17);

			Map<String, String> groupMap18 = new HashMap<String, String>();
			groupMap18.put("key", "2");
			groupMap18.put("group", "配置IP,MAC绑定列表");
			groupMap18.put("activityName", "");
			groups.add(groupMap18);

			Map<String, String> groupMap19 = new HashMap<String, String>();
			groupMap19.put("key", "2");
			groupMap19.put("group", "文档安全配置");
			groupMap19.put("activityName", "");
			groups.add(groupMap19);
		}

		childs.clear();
		for (int i = 0; i < groups.size(); i++) {
			List<Map<String, String>> varList = new ArrayList<Map<String, String>>(0);
			childs.add(varList);
		}

		DataSetUtil.checkDataSource(this.groups, this.childs);
	}

	/**
	 *
	 */
	Handler backHandler = new Handler(){
		public void handleMessage(Message msg){
			isBack = false;
		}
	};
	@Override
	public void onBackPressed() {
		LoginInfo loginInfo = LoginSharedPreferencesHelper.getLoginInfo(getApplicationContext());
		if (!isTop) {
			initViewSetting();
		} else if (!isBack) {
			isBack = true;
			if(loginInfo.isAutoLogin()){
				Toast.makeText(getApplicationContext(), "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(getApplicationContext(), "再按一次到登录界面", Toast.LENGTH_SHORT).show();
			}
			backHandler.sendEmptyMessageDelayed(0, 2000);
		} else {
			if(loginInfo.isAutoLogin()){
				finish();
				System.exit(0);
			}else{
				finish();
				startActivity(new Intent(ConfigActivity.this, LoginActivity.class));
			}
		}
	}
}
